//
//  MainTabBarViewController.swift
//  BW新浪微博
//
//  Created by 静静静 on 15/9/1.
//  Copyright © 2015年 BossKai. All rights reserved.
//

import UIKit

class MainTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addchildViewControllers()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        setupComposedButton()
    }
    
    private func setupComposedButton(){
        let count = viewControllers?.count
        let w = UIScreen.mainScreen().bounds.width/CGFloat(count!)
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        composedButton.frame = CGRectOffset(rect, 2*w, 0)
        
    }
   
    private func addchildViewControllers()
    {
        addChildViewController(HomeTableViewController(), title: "首页", iconName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", iconName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", iconName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", iconName: "tabbar_profile")
    }
    
    private func addChildViewController(vc:UIViewController,title:String,iconName:String ) {
        
        
        let nav = UINavigationController(rootViewController: vc)
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: iconName)
       
        addChildViewController(nav)
    }
    
    
    
    // MARK: - 懒加载
    private lazy var composedButton: UIButton = {
    
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(btn)
        
        btn.addTarget(self, action: "composedBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    @objc private func composedBtnClick(){
        print("哎呀")
    }

}
